#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.IO;
using NUnit.Framework;
using Newtonsoft.Utilities.IO;
using Newtonsoft.Utilities.IO.FileSystem;
using Newtonsoft.Utilities.Time;
using System.Security.AccessControl;

namespace Newtonsoft.Utilities.Tests.IO.FileSystem
{
  public class FileTests : TestsBase
  {
    [Test]
    public void DeleteFile()
    {
      string currentPath = FileUtils.MapPath(@"~/");
      string testFilePath = Path.Combine(currentPath, "testfile.txt");

      FileUtils.EnsureFileDeleted(testFilePath);

      IFileSystem fileSystem = new Utilities.IO.FileSystem.FileSystem(MockClock.Instance);
      IFile file = fileSystem.GetFile(testFilePath);

      Assert.AreEqual(false, file.Exists);

      using (Stream s = file.Create())
      using (TextWriter tw = new StreamWriter(s))
      {
        tw.Write("text");
      }

      Assert.AreEqual(true, file.Exists);

      file.Delete();
      Assert.AreEqual(false, file.Exists);
    }

    [Test]
    public void CopyFile()
    {
      string currentPath = FileUtils.MapPath(@"~/");
      string testFilePath = Path.Combine(currentPath, "testfile.txt");
      string copiedFilePath = Path.Combine(currentPath, "copiedfile.txt");

      FileUtils.EnsureFileDeleted(testFilePath);
      FileUtils.EnsureFileDeleted(copiedFilePath);

      IFileSystem fileSystem = new Utilities.IO.FileSystem.FileSystem(MockClock.Instance);
      IFile file = fileSystem.GetFile(testFilePath);

      Assert.AreEqual(false, file.Exists);

      using (Stream s = file.Create())
      using (TextWriter tw = new StreamWriter(s))
      {
        tw.Write("text");
      }

      Assert.AreEqual(true, file.Exists);

      IFile copiedFile = fileSystem.GetFile(copiedFilePath);

      Assert.AreEqual(false, copiedFile.Exists);

      file.CopyTo(copiedFile.FullName);
      Assert.AreEqual(true, copiedFile.Exists);
    }

    [Test]
    public void GetAccessControl_ShouldReturnFileSecurity()
    {
      string currentPath = FileUtils.MapPath(@"~/");
      string testFilePath = Path.Combine(currentPath, "testfile.txt");

      FileUtils.EnsureFileDeleted(testFilePath);
      FileUtils.EnsureFileExists(testFilePath);

      IFileSystem fileSystem = new Utilities.IO.FileSystem.FileSystem(MockClock.Instance);
      IFile file = fileSystem.GetFile(testFilePath);

      FileSecurity fileSecurity = file.GetAccessControl();
      Assert.IsNotNull(fileSecurity);
    }
  }
}